package com.baozun.houji.me.viewmodel

import androidx.lifecycle.MutableLiveData
import com.baozhun.mall.common.model.bean.AddressInfo
import com.baozhun.mall.common.model.bean.HotRegionBean
import com.baozhun.mall.common.model.bean.RegionBean
import com.baozhun.mall.common.util.CacheUtil
import com.baozun.houji.me.network.mMeApiService
import me.hgj.jetpackmvvm.base.viewmodel.BaseViewModel
import me.hgj.jetpackmvvm.ext.request

/**
 * @Title：
 * @Description：
 * @Author：xing.luo
 * @Since：2022/12/16
 * @Version：1.0.0
 */
class ModifyAddressViewModel : BaseViewModel() {
    val defaultResult = MutableLiveData<Any>()
    val hotRegionList = MutableLiveData<MutableList<HotRegionBean>>()
    val regionList = MutableLiveData<MutableList<RegionBean>>()

    fun getHotRegion() {
        request({ mMeApiService.getHotAddressRegion() }, {
            hotRegionList.value = it
        }, {

        }, isShowDialog = false)
    }

    fun getRegion() {
        val cacheList = CacheUtil.getRegionListCache()
        if (cacheList.isNotEmpty()) {
            regionList.value = cacheList
        }
        request({ mMeApiService.getAddressRegion() }, {
            if(regionList.value.isNullOrEmpty()) regionList.value = it
            CacheUtil.saveRegionList(it)
        }, {

        }, isShowDialog = false)
    }

    fun saveAddress(addressInfo: AddressInfo) {
        request({ mMeApiService.saveAddress(addressInfo) }, {
            defaultResult.postValue(it)
        }, {

        }, isShowDialog = true)
    }

    fun deleteAddress(id: String) {
        request({ mMeApiService.deleteAddress(id) }, {
            defaultResult.postValue("ok")
        }, {

        }, isShowDialog = true)
    }
}